/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pers.sakurasora.lesson.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.sakurasora.lesson.dao.ProductDao;
import pers.sakurasora.lesson.entity.BizProduct;
import pers.sakurasora.lesson.service.ProductService;

/**
 * @author SakuraSora
 * @Email 1468071675@qq.com
 * @DateTime 2018-11-2 10:43:29
 *
 * @Description
 *              服务层(业务逻辑层)实现类--商品
 */
@Service /*标注它是一个Service组件;
           需要在applicationContext.xml文件中配置<context:component-scan base-package="pers.sakurasora.lesson.service.impl"/>,
           注记才会生效，才会作为一个组件注册到spring的容器中去*/
public class ProductServiceImpl implements ProductService{

    @Autowired//自动依赖注入--业务逻辑层要依赖于DAO层，去访问DAO层
    ProductDao dao;
    
    @Override
    public List<BizProduct> getAllProducts() {
        return dao.selecAll();
    }

    @Override
    public boolean addProduct(BizProduct product) {
        return dao.insert(product) == 1;
    }

    @Override
    public boolean decreaseStock(int productId, int account) {
        BizProduct product = dao.selectByPrimaryKey(productId);
        product.setStock(product.getStock() - account);
        return dao.update(product) == 1;
    }

}
